import 'dart:typed_data';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
// import 'package:flutter_file_adapter/flutter_file_adapter.dart';

const env = String.fromEnvironment('ENV', defaultValue: 'dev');
String prefix = 'assets';

/// 初始化图片请求地址，常用于设置cdn
void setAssetPrefix(String url) {
  prefix = '${url}assets';
}

Widget imageFromAsset(
  String asset, {
  Alignment alignment = Alignment.center,
  Color? color,
  BoxFit fit = BoxFit.contain,
  double? height,
  Key? key,
  String? package,
  WidgetBuilder? placeholderBuilder,
  String? semanticsLabel,
  double? width,
  ImageFrameBuilder? frameBuilder, //无用
}) {
  final String imageUrl = package == null || package.isEmpty ? '$prefix/$asset' : '$prefix/packages/$package/$asset';
  return CachedNetworkImage(
    imageUrl: imageUrl,
    alignment: alignment,
    color: color,
    placeholder: (
      BuildContext context,
      String url,
    ) {
      // web 没有办法调用这个方法
      // return placeholderBuilder(context);
      return Container();
    },
    fit: fit,
    width: width,
  );
}

Widget svgFromAsset(
  String asset, {
  Alignment alignment = Alignment.center,
  Color? color,
  bool excludeFromSemantics = false,
  BoxFit fit = BoxFit.contain,
  double? height,
  Key? key,
  String? package,
  WidgetBuilder? placeholderBuilder,
  String? semanticsLabel,
  double? width,
  ImageFrameBuilder? frameBuilder,
}) {
  // web中，WebsafeSvg会将图片下载下来转成编码成base64，在大批量图片处理时，性能损耗大,且无法最大限度利用浏览器图片缓存优化，这个过程在web中没必要。
  final String imageUrl = package == null || package.isEmpty ? '$prefix/$asset' : '$prefix/packages/$package/$asset';
  return CachedNetworkImage(
    imageUrl: imageUrl, // '$prefix/packages/$package/$asset',
    alignment: alignment,
    color: color,
    placeholder: (
      BuildContext context,
      String url,
    ) {
      // web 没有办法调用这个方法
      // return placeholderBuilder(context);
      return Container();
    },
    fit: fit,
    width: width,
  );
}

///浏览器没有只能使用 memory
Widget svgFromFile({
  String? imageUrl,
  double? width,
  double? height,
  Color? color,
  BoxFit? fit,
  WidgetBuilder? placeholderBuilder,
}) {
  return FutureBuilder<Uint8List?>(
    future: _getSvgData(imageUrl),
    builder: (context, snapshot) {
      if (snapshot.hasData && snapshot.data != null) {
        return Container();
      }
      return placeholderBuilder!(context);
    },
  );
}

///todo 加载 file中的svg 资源
Future<Uint8List?> _getSvgData(String? imageUrl) async {
  // File file = File(imageUrl);
  // if (!file.existsSync()) {
  //   return null;
  // }
  // return file.readAsBytes();
  return null;
}

// Widget imageFromFile(
//   String path, {
//   Alignment alignment = Alignment.center,
//   Color? color,
//   BoxFit fit = BoxFit.contain,
//   double? height,
//   Key? key,
//   String? package,
//   WidgetBuilder? placeholderBuilder,
//   String? semanticsLabel,
//   double? width,
//   ImageFrameBuilder? frameBuilder, //无用
//   String? placeHoldUrl,
//   bool loadingAnimation = false,
//   Widget? placeHoldWidget,
// }) {
//   return FutureBuilder<String>(
//     future: GlobalFileStore.instance.globalPath(path),
//     builder: (context, snapshot) {
//       return CachedNetworkImage(
//         imageUrl: snapshot.data ?? '',
//         alignment: alignment,
//         color: color,
//         placeholder: (
//           BuildContext context,
//           String url,
//         ) {
//           // web 没有办法调用这个方法
//           // return placeholderBuilder(context);
//           return Container();
//         },
//         fit: fit,
//         width: width,
//       );
//     },
//   );
// }
